package com.programmercarl.array;

/**
 * https://leetcode.cn/problems/backspace-string-compare/description/
 *
 * @author yulongtian
 * @create 2024-12-10 14:59
 */
public class Test09 {
    public static void main(String[] args) {
        String s = "ab#c";
        String t = "ad#c";
        System.out.println(backspaceCompare(s, t));
    }

    public static boolean backspaceCompare(String s, String t) {
        char[] chs_s = delete(s);
        char[] chs_t = delete(t);
        if (chs_s.length != chs_t.length)
            return false;
        for (int i = 0; i < chs_s.length; i++) {
            if (chs_s[i] != chs_t[i]) {
                return false;
            }
        }
        return true;
    }

    public static char[] delete(String s) {
        char[] chs_s = new char[s.length()];
        int s_p = 0;
        for (int i = 0; i < chs_s.length; i++) {
            if (s.charAt(i) == '#') {
                if (s_p != 0) {
                    s_p--;
                }
            } else {
                chs_s[s_p++] = s.charAt(i);
            }
        }
        char[] chars = new char[s_p];
        for (int i = 0; i < chars.length; i++) {
            chars[i] = chs_s[i];
        }
        return chars;
    }
}
